package top.tangyh.lamp.care.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.lamp.care.mapper.BCareHandoverMapper;
import top.tangyh.lamp.care.service.BCareHandoverService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BCareHandoverManager;
import top.tangyh.lamp.care.entity.BCareHandover;
import top.tangyh.lamp.care.vo.query.CareHoverPageParam;
import top.tangyh.lamp.care.vo.result.PageOutputDTO;
import top.tangyh.lamp.care.vo.save.BCareHandoverSaveVO;
import top.tangyh.lamp.care.vo.update.BCareHandoverUpdateVO;
import top.tangyh.lamp.care.vo.result.BCareHandoverResultVO;
import top.tangyh.lamp.care.vo.query.BCareHandoverPageQuery;
import top.tangyh.lamp.common.utils.ConvertUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 业务实现类
 * 护理交班
 * </p>
 *
 * @author zqb
 * @date 2024-07-26 17:27:29
 * @create [2024-07-26 17:27:29] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BCareHandoverServiceImpl extends SuperServiceImpl<BCareHandoverManager, Long, BCareHandover> implements BCareHandoverService {

    private final BCareHandoverMapper bCareHandoverMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOrUpdateCareHandOver(BCareHandover careHandoverPO) {
        //本人编辑 或者判断是否是存在交班内容提醒
        QueryWrapper<BCareHandover> queryWrapper = new QueryWrapper();
        queryWrapper.eq("shift_name", careHandoverPO.getShiftName());
        //未删除的
        queryWrapper.eq("is_deleted", 0);
        queryWrapper.eq("patient_ident", careHandoverPO.getPatientIdent());
        queryWrapper.apply("date_format(hover_time,'%Y-%m-%d') = DATE_FORMAT('" + careHandoverPO.getHoverTime() + "','%Y-%m-%d')");
        BCareHandover one = bCareHandoverMapper.selectOne(queryWrapper);

        if (null != careHandoverPO.getId()) {   //编辑的时候
            if (null != one) {
                if (!one.getId().equals(careHandoverPO.getId())) {
                    return -1;
                    //已存在交班记录
                    //  throw new BadRequestException("已存在交班记录,"+one.getId());
                } else {
                    return bCareHandoverMapper.updateById(careHandoverPO);
                }

            }

        } else {    //新增的时候
            if (null == one) {
                return bCareHandoverMapper.insert(careHandoverPO);
                //  return ApiR.success(this.save(careHandoverPO));
            }else{
                return -1;
            }
            //已存在交班记录
            //  throw new BadRequestException("已存在交班记录,"+one.getId());
        }
        return -2;
    }


    @Override
    public PageOutputDTO<BCareHandoverResultVO> getListAll(CareHoverPageParam careHoverPageParam){
        List<BCareHandover> list;
        QueryWrapper<BCareHandover> orgInfoQueryWrapper = new QueryWrapper<>();
        orgInfoQueryWrapper.eq("patient_ident", careHoverPageParam.getPatientIdent());
        orgInfoQueryWrapper.eq("hospital_number", careHoverPageParam.getHospitalNumber());
        orgInfoQueryWrapper.eq("is_deleted", 0);

        orgInfoQueryWrapper.like(StringUtils.isNotBlank(careHoverPageParam.getHoverName()), "hover_name", careHoverPageParam.getHoverName());
        //1日度查询当前日的数据
        //0月度 查询当前月的数据
        if (careHoverPageParam.getMonthOrDay() == 0) {
            orgInfoQueryWrapper.apply("date_format(hover_time,'%Y%m') = DATE_FORMAT('" + careHoverPageParam.getHoverTime() + "','%Y%m')");
        } else if (careHoverPageParam.getMonthOrDay() == 1) {
            orgInfoQueryWrapper.apply("TO_DAYS(hover_time) = TO_DAYS('" + careHoverPageParam.getHoverTime() + "')");
        }
        orgInfoQueryWrapper.select().orderByDesc("hover_time");

        IPage<Map<String, Object>> page = new Page<>(careHoverPageParam.getPage(), careHoverPageParam.getSize());


        IPage<Map<String,Object>> categoryEntityIPage = bCareHandoverMapper.selectMapsPage(page,orgInfoQueryWrapper);


        System.out.println("总页数"+categoryEntityIPage.getPages());
        System.out.println("总记录数"+categoryEntityIPage.getTotal());
        List<Map<String,Object>> records = categoryEntityIPage.getRecords();
        return new PageOutputDTO<>(categoryEntityIPage.getTotal(), categoryEntityIPage.getPages(),  categoryEntityIPage.getCurrent(),
                ConvertUtil.convertList(records, BCareHandoverResultVO.class));
    }


    @Override
    public List<BCareHandover> getTwoDayList(String hospitalNumber, String patientIdent) {
        List<BCareHandover> list = new ArrayList<>();
        QueryWrapper<BCareHandover> orgInfoQueryWrapper = new QueryWrapper<>();
        orgInfoQueryWrapper.eq("patient_ident", patientIdent);
        orgInfoQueryWrapper.eq("hospital_number", hospitalNumber);
        orgInfoQueryWrapper.eq("is_deleted", 0);

        Calendar calendar1 = Calendar.getInstance();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        calendar1.add(Calendar.DATE, -2);
        String two_days_ago = sdf1.format(calendar1.getTime());
        orgInfoQueryWrapper.apply("date_format(hover_time,'%Y-%m-%d') >= '" + two_days_ago + "'");
        list = this.list(orgInfoQueryWrapper);
        return list;
    }
}


